package com.zhilianbao.leyaogo.ui.fragment.me.myaccount;

import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.bql.utils.EventManager;
import com.bql.utils.ToastUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.DialogCallback;
import com.zhilianbao.leyaogo.ui.fragment.base.BaseOkHttpFragment;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.utils.XTipsDialog;
import com.zhilianbao.leyaogo.utils.XToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liu on 2016/11/5.
 * 激活卡券
 */
public class ActivateCardVouvherFragment extends BaseOkHttpFragment {
    @BindView(R.id.card_voucher_list)
    EditText mCardVoucherList;
    @BindView(R.id.card_voucher_pwd)
    EditText mCardVoucherPwd;
    @BindView(R.id.tv_activate)
    TextView mTvActivate;

    public static ActivateCardVouvherFragment getInstance() {
        ActivateCardVouvherFragment fragment = new ActivateCardVouvherFragment();
        return fragment;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
    }

    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.card_voucher));
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_add_card;
    }

    @OnClick(R.id.tv_activate)
    public void onClick() {
        checkCardListAndPwd();
    }

    private void checkCardListAndPwd() {
        String cardVocherList = mCardVoucherList.getText().toString().trim();
        String cardVocherPwd = mCardVoucherPwd.getText().toString().trim();
        long userId = Utils.getUserLoginInfo().getUserId();
       if (cardVocherList == null ||cardVocherList.equals("")){
           XToastUtils.showShortToast("请输入卡券序列号");
            //让账号输入框获取焦点
            mCardVoucherList.requestFocus();
        }else if (cardVocherPwd == null || cardVocherPwd.equals("")) {
           XToastUtils.showShortToast("请输入卡券密码");
            //让密码输入框获取焦点
            mCardVoucherPwd.requestFocus();
        }else {
           AccountApi.activationRechargeCard(mActivity, userId, cardVocherList, cardVocherPwd, new DialogCallback<Object>(mActivity) {
               @Override
               public void onSuccess(Object o, Call call, Response response) {
                   XToastUtils.showShortToast("卡券激活成功");
                   pop();
                   EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_CARD_VOUCHER_ACTIVATE_SUCCESS));
               }
               @Override
               public void onError(Call call, Response response, Exception e) {
                   super.onError(call, response, e);
//                   if (e.getMessage().equals("10028")) {
//                       XToastUtils.showShortToast("序列号错误");
//                   }
                   XToastUtils.showShortToast("序列号或密码输入有误");

                   //清除输入框里的内容
//                   Editable list = mCardVoucherList.getText();
//                   Editable pwd = mCardVoucherPwd.getText();
//                   pwd.delete(0,pwd.length());
//                   list.delete(0,list.length());
                   //让账号输入框获取焦点
                   mCardVoucherList.requestFocus();
               }
           });

        }
    }
}
